from __init__ import log
from executor import Executor as _Executor
from xml.sax.saxutils import escape
from xml.dom import minidom
from gobject import idle_add

class Executor(_Executor):
    def execute(self, input, main=True):
        self.input = input
        if main:
            log.start()
        log('<type>Text</type>')
        
        commands =  input.getCommands()
        log('<input>%s</input>'%escape('<subinput>'.join(commands)))
        
        subtext = []
        for i in input.subinputs:
            log('<subinput>')
            subtext.append(i[0].__execute_instantly__())
            log('</subinput>')
        
        subtext.append('')    
        buf = []
        for i in range(len(commands)):
            buf.append(escape(commands[i]))
            buf.append(subtext[i].strip(' \t\n')) 
        if input.ancestor.identifier=='Input':
            buf.append('\n')
        text = ''.join(buf)
        log('<output>%s</output>'%text)
        
        if main:
            DOM = minidom.parseString('<output>%s</output>'%text).documentElement
            idle_add(input.ancestor.call, 'Output', 'setdom', DOM)
            self.input = None
            log.end()
        else:
            return text
        